package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.entity.Blog;
import com.itheima.domain.vo.LoginUserInfo;
import com.itheima.domain.vo.Result;
import com.itheima.service.IBlogService;
import com.itheima.utils.ThreadUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-10-06
 */
@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {
    @Resource
    private IBlogService blogService;

    /**
     * 查询热门博客列表
     */
    @GetMapping("/hot")
    public Result getHotBlogs(@RequestParam(value = "current",defaultValue = "1") Long current){
        log.info("查询的页码为：{}",current);
        List<Blog> blogs = blogService.getHotBlogs(current);
        return Result.ok(blogs);
    }

    /**
     * 为某篇博客点赞
     */
    @PutMapping("/like/{blogId}")
    public Result likeOneBlog(@PathVariable("blogId") Long blogId){
        log.info("点赞的博客id为：{}",blogId);
        blogService.likeOneBlog(blogId);
        return Result.ok();
    }

    /**
     * 查询某个博客的主页
     */
    @GetMapping("/{blogId}")
    public Result queryBlogInfo(@PathVariable("blogId") Long blogId){
        log.info("要查询的博客的id为：{}",blogId);
        return blogService.queryBlogInfo(blogId);
    }

    /**
     * 查询某个博客点赞排名前五的用户
     */
    @GetMapping("/likes/{blogId}")
    public Result queryBlogLikesTop5(@PathVariable("blogId") Long blogId){
        log.info("当前查询的博客id为：{}",blogId);
        return blogService.queryBlogLikesTop5(blogId);
    }

    /**
     * 查询自己的博客
     */
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        LoginUserInfo loginUserInfo = ThreadUtil.get();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", loginUserInfo.getUserId()).page(new Page<>(current, 10));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /**
     * 查询自己关注博主的博客
     */
    @GetMapping("/of/follow")
    public Result queryMyFollowBlog(@RequestParam("lastId") Long max,
                                    @RequestParam(value = "offset",defaultValue = "0") Integer offset) {
        log.info("要查询的最大时间戳：{}，偏移量为：{}",max,offset);
        return blogService.queryMyFollowBlog(max,offset);
    }

    /**
     * 发布探店笔记,发布的同时会把博客推送到粉丝邮箱
     */
    @PostMapping
    public Result sendBlogWithShop(@RequestBody Blog blog){
        log.info("探店笔记内容为：{}",blog);
        return blogService.sendBlogWithShop(blog);
    }

    /**
     * 查看博客作者的作品
     */
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, 10));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }
}
